// pages/userInfoCenter/index.js
const App = getApp();
const {Dialog, Toast} = require("../../../../config/modal")
const utils = require("../../../../utils/util")
const {LOGIN, LOCATION} = require("../../../../config/login.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isStartMp: App.$isStartMp(), // 是否显示公众号图片
        publicImg: 'http://aliyun.thisforyou.cn:9004/cbd-prod/mini/public.png',
        publicImg2: 'http://aliyun.thisforyou.cn:9004/cbd-prod/mini/public2.jpg',
        showQrCodeDialog: false,
        isAuth: App.$isAuth(),
        bodyBgColor: "#fdfcfc",
        theamTextColor: App.globalData.theamTextColor,
        userInfo: {
            province: "请先授权",
            avatarUrl: '',
            school: '',
            nickName: '',
            inviteCode: '',
            integral: 0,
        },
        message: {
            count: 0,
        },
        window: {},
        functions: [{
            image: "/static/function/package.png",
            text: "代取快递",
            page: "myAcceptPackage",
            open: true,
        }, {
            image: "/static/function/task.png",
            text: "帮人代办",
            page: "myAcceptTask",
            open: true,
        }],
        MyPushfunctions: [{
            image: "/static/function/package.png",
            text: "找人取快递",
            page: "packagePush",
            open: true,
        }, {
            image: "/static/function/task.png",
            text: "找人代办",
            page: "taskPush",
            open: true,
        }, {
            image: "/static/function/zahuo.png",
            text: "二手物品",
            page: "myProduct",
            open: true,
        }, {
            image: "/static/function/recruit.png",
            text: "兼职招聘",
            page: "myPartTimeJob",
            open: true,
        }],


        otherFunctions2: [
            {
                image: "/static/function/share.png",
                text: "邀请有礼",
                page: "sharePage",
                open: true,
                auth: false,
            },
            {
                image: "/static/function/integral.png",
                text: "积分好物",
                page: "exchange",
                open: true,
                auth: false,
            },
            {
                image: "/static/function/question.png",
                text: "问题帮助",
                page: "question",
                open: true,
                auth: false,
            }
        ],

        otherFunctions: [
            {
                image: "/static/function/address.png",
                text: "地址管理",
                page: "address",
                open: true,
                type: "",
                auth: true
            },
            {
                image: "/static/function/back.png",
                text: "意见反馈",
                page: "feedback",
                open: true,
                type: "",
                auth: true
            },

            {
                image: "/static/function/abaout.png",
                text: "关于我们",
                page: "about",
                open: true,
                type: "",
                auth: false
            },
            {
                image: "/static/function/setting.png",
                text: "设置",
                page: "",
                open: true,
                auth: false,
                type: "setting",
            }
        ]
    },
    URL: {
        getInviteCode: "/customer/getInviteCode",
        getIntegralAll: "/integral/getIntegralAll",
        noRead: "/message/noRead"
    },
    page: {
        question: '/pages/question-help/index',
        exchange: "/pages/tabbar/user/integral/exchange/index",
        sharePage: '/pages/invite/index',
        indexPage: "/pages/tabbar/index/index",
        userInfoEdit: '/pages/tabbar/user/userInfoEdit/index',
        userIntegral: '/pages/tabbar/user/integral/index',
        address: "/pages/tabbar/user/address/index",
        taskPush: "/pages/tabbar/user/userInfoCenter/myTask/index",
        packagePush: "/pages/tabbar/user/userInfoCenter/myPackages/index",
        myAcceptTask: "/pages/tabbar/user/userInfoCenter/myAcceptTask/index",
        myAcceptPackage: "/pages/tabbar/user/userInfoCenter/myAcceptPackage/index",
        about: '/pages/about-me/index',
        target: '/pages/tabbar/user/userInfoCenter/index',
        feedback: '/pages/tabbar/user/feedback/index',
        message: '/pages/tabbar/user/userInfoCenter/message/index',
        myProduct: '/pages/tabbar/user/userInfoCenter/myProduct/index',
        myPartTimeJob: '/pages/recruit/list/index',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        App.$handlerInviteCode(options);

        this.setData({
            window: App.globalData.window
        })
        this.setData({
            'publicImg': this.data.publicImg + "?v=" + new Date().getTime()
        })
    },

    onShow() {
        this.initUserInfo();
        if (App.$isAuth()) {
            this.getNoReadMessage();
            this.initIntegralAll();
        }
        this.setData({
            isStartMp: App.$isStartMp()
        })
    },
    clickAvatar(e) {
        if (!App.$isAuth()) {
            App.$navigateToAuth()
            return;
        }
        let imgUrl = e.currentTarget.dataset.img;
        let imgUrls = [imgUrl];
        wx.previewImage({
            current: imgUrl,
            urls: imgUrls
        })
    }

    , showLagerImg(e) {
        let imgUrl = e.currentTarget.dataset.img;
        let imgUrls = [imgUrl];
        wx.previewImage({
            current: imgUrl,
            urls: imgUrls
        })
    },

    getNoReadMessage() {
        App.$get(false, this.URL.noRead, {}, res => {
            if (res.R) {
                this.setData({
                    'message.count': res.data
                })
            }

        })
    },

    initIntegralAll() {
        App.$get(false, this.URL.getIntegralAll, {}, res => {
            if (res.R) {
                this.setData({
                    "userInfo.integral": res.data
                })
            }
        })
    },

    getInviteCode() {
        this.setData({
            showQrCodeDialog: true,
        })
    },

    // 弹出二维码
    showQrCodeDialog() {
        if (!App.$isAuth()) {
            App.$navigateToAuth();
            return;
        }
        this.getInviteCode();
    },
    onCloseDialog() {
        this.setData({
            showQrCodeDialog: false,
        })
    },

    // 任务
    toJumFunctionPage(e) {
        let isOpen = e.currentTarget.dataset.open;
        if (isOpen) {
            let page = e.currentTarget.dataset.page;
            App.$navigateTo(this.page[page], true);
            return;
        }
        Toast.fail('开发君，💪开发中，尽请期待!!!!')
    },
    // 其他功能
    toJumOtherFunctionPage(e) {
        let isOpen = e.currentTarget.dataset.open;
        let type = e.currentTarget.dataset.type;
        if (isOpen && type == "setting") {
            wx.openSetting({})
            return;
        }
        if (isOpen) {
            let page = e.currentTarget.dataset.page;
            let auth = e.currentTarget.dataset.auth;
            App.$navigateTo(this.page[page], auth);
            return;
        }
        Toast.fail('开发君，💪开发中，尽请期待!!!!')
    },
    // 编辑用户信息
    toEditUserInfo() {
        App.$navigateTo(this.page.userInfoEdit, true)
    },
    // 进入积分页面
    toMyIntegral() {
        App.$navigateTo(this.page.userIntegral, true)
    },

    // 消息页面
    toMessage() {
        App.$navigateTo(this.page.message, true)
    },

    // 初始化用户信息
    initUserInfo() {
        let userInfo = App.$getUserInfo();
        if (!utils.isInvalid(userInfo)) {
            this.setData({
                "userInfo.province": utils.isInvalid(userInfo.citySure) ? "请先授权" : (userInfo.provinceSure + " " + userInfo.citySure),
                "userInfo.avatar": userInfo.avatar,
                "userInfo.realName": userInfo.realName,
                "userInfo.nickName": userInfo.nickName,
                "userInfo.school": userInfo['schoolName'],
                "userInfo.userCode": userInfo['userCode'],
                "userInfo.inviteCode": userInfo['inviteCode'],
                "userInfo.inviteQrCode": userInfo['inviteQrCode'],
            })
        }
        this.setData({
            isAuth: App.$isAuth()
        })
    },

    // 点击授权地理位置
    toAuthLocation() {
        LOCATION.toAuthLocation((res) => {
            this.initUserInfo();
        })
    },
    goBack() {
        App.$switchTab(this.page.indexPage, false);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return App.$onShare(this.page.target, null, "校园CBD")
    },
    showPublicImg() {
        let imgUrl = this.data.publicImg2 + "?v=" + new Date().getTime();
        let imgUrls = [imgUrl];
        wx.previewImage({
            current: imgUrl,
            urls: imgUrls
        })
    },


})
